<template>
  <div>
    <el-table
      v-loading="loading"
      :data="permissionArr"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="code" label="权限值"></el-table-column>
      <el-table-column prop="updateTime" label="修改时间"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button
            type="primary"
            size="small"
            v-show="row.level != 4"
            @click="addPermission(row)"
            v-has="`btn.Permission.add`"
          >
            {{ row.level == 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button
            type="warning"
            size="small"
            :disabled="row.level == 1"
            @click="updatePermission(row)"
            v-has="`btn.Permission.update`"
          >
            编辑
          </el-button>
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            width="180px"
            icon="Delete"
            icon-color="red"
            :title="`你确定要删除&nbsp;${row.name}&nbsp;吗?`"
            @confirm="removeMenu(row)"
            v-has="`btn.Permission.remove`"
          >
            <template #reference>
              <el-button type="danger" size="small" :disabled="row.level == 1">
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加/修改菜单对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="menuData.id ? '修改菜单' : '添加菜单'"
      width="430px"
    >
      <el-form label-width="90px">
        <el-form-item :label="menuData.level == 4 ? '功能名称' : '菜单名称'">
          <el-input
            v-model.trim="menuData.name"
            :placeholder="
              menuData.level == 4 ? '请输入功能名称' : '请输入菜单名称'
            "
          />
        </el-form-item>
        <el-form-item label="权限值：">
          <el-input v-model.trim="menuData.code" placeholder="请输入权限值" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveMenu">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="Permission">
import {
  reqAddOrUpdateMenu,
  reqAllPermission,
  reqRemoveMenu,
} from '@/api/acl/menu'
import { MenuParams, Permission } from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'

// 控制是否显示加载动画
let loading = ref(false)
// 存储菜单和按钮
let permissionArr = ref<Permission[]>([])
// 控制添加/修改菜单对话框的显示/隐藏
let dialogVisible = ref(false)
// 收集添加/修改菜单对话框中输入的数据
const menuData = reactive<MenuParams>({
  id: undefined, //修改菜单的id
  name: '', // 权限名称
  code: '', // 权限值
  level: 0, // 菜单的等级
  pid: 0, // 菜单父级id
})

// 获取已有菜单和按钮的方法
const getHasPermission = async () => {
  // 显示加载动画
  loading.value = true
  const result = await reqAllPermission()
  if (result.code === 200) {
    // 存储菜单和按钮
    permissionArr.value = result.data
    // 关闭加载动画
    loading.value = false
  } else {
    ElMessage.error('获取菜单和按钮失败')
  }
}
// 点击添加菜单按钮的回调
const addPermission = (row: Permission) => {
  // 清空上一次收集的数据
  Object.assign(menuData, {
    id: undefined, //修改菜单的id
    name: '', // 权限名称
    code: '', // 权限值
    level: 0, // 菜单的等级
    pid: 0, // 菜单父级id
  })
  // 获取level和pid
  menuData.level = row.level + 1 // 新增的子菜单的等级等于上一级菜单等级+1
  menuData.pid = row.id as number // 新增菜单的父级id
  // 显示对话框
  dialogVisible.value = true
}
// 点击编辑按钮的回调
const updatePermission = (row: Permission) => {
  // 当前菜单数据
  Object.assign(menuData, row)
  // 显示对话框
  dialogVisible.value = true
}
// 点击对话框中确定按钮的回调
const saveMenu = async () => {
  // 菜单/功能名和权限值非空判断
  if (menuData.name == '' || menuData.code == '') {
    ElMessage.warning('菜单/功能名和权限值不能为空！！')
    return
  }
  // 发请求
  const result = await reqAddOrUpdateMenu(menuData)
  console.log(result)
  if (result.code == 200) {
    ElMessage.success(menuData.id ? '修改成功！' : '添加成功！')
    // 关闭对话框
    dialogVisible.value = false
    // 重新获取菜单数据
    getHasPermission()
  }
}
// 点击确定删除按钮的回调
const removeMenu = async (row: Permission) => {
  // 发请求
  const result = await reqRemoveMenu(row.id as number)
  if (result.code == 200) {
    ElMessage.success('删除成功！')
    // 重新获取菜单数据
    getHasPermission()
  } else {
    ElMessage.error('删除失败！')
  }
}

// 组件挂载时获取全部菜单和按钮数据
onMounted(() => {
  getHasPermission()
})
</script>

<style scoped></style>
